package com.easyapi.service;

import com.easyapi.common.bean.ApiCenterInterfaceInfo;
import com.easyapi.common.emun.ApiOperationEnum;
import com.easyapi.common.emun.InterfaceInfoEnum;
import com.easyapi.common.emun.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.config.DynamicApiConfig;
import com.easyapi.mapper.ApiCenterInterfaceInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: javadadi
 * @Time: 20:33
 * @ClassName: ApiService
 */
@Slf4j
@Resource
public class ApiService {
    @Resource
    private ApiCenterInterfaceInfoMapper interfaceInfoMapper;

    @Resource
    private DynamicApiConfig dynamicApiConfig;

    public void onlineGet(Long id) {
        if (id <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterInterfaceInfo interfaceInfo = interfaceInfoMapper.selectById(id);
        if (Objects.isNull(interfaceInfo)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<String> registered = dynamicApiConfig.getAlreadyRegistered(RequestMethod.GET);
        String apiPath = interfaceInfo.getApiPath();
        if (registered.contains(apiPath)) {
            return;
        }
        List<String> paths = new ArrayList<>();
        paths.add(apiPath);
        try {
            dynamicApiConfig.registerDynamicApiRoutesGET(paths);
        } catch (NoSuchMethodException e) {
            log.error(e.getMessage());
        }
    }

    public void offlineGet(Long id) {
        if (id <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterInterfaceInfo interfaceInfo = interfaceInfoMapper.selectById(id);
        if (Objects.isNull(interfaceInfo)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<String> registered = dynamicApiConfig.getAlreadyRegistered(RequestMethod.DELETE);
        String apiPath = interfaceInfo.getApiPath();
        if (registered.contains(apiPath)) {
            return;
        }
        List<String> paths = new ArrayList<>();
        paths.add(apiPath);
        dynamicApiConfig.unregisterDynamicApiRoutesGET(paths);
    }

    public void onlinePost(Long id) {
        if (id <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterInterfaceInfo interfaceInfo = interfaceInfoMapper.selectById(id);
        if (Objects.isNull(interfaceInfo)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<String> registered = dynamicApiConfig.getAlreadyRegistered(RequestMethod.POST);
        String apiPath = interfaceInfo.getApiPath();
        if (registered.contains(apiPath)) {
            return;
        }
        List<String> paths = new ArrayList<>();
        paths.add(apiPath);
        try {
            dynamicApiConfig.registerDynamicApiRoutesPOST(paths);
        } catch (NoSuchMethodException e) {
            log.error(e.getMessage());
        }
    }

    public void offlinePost(Long id) {
        if (id <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterInterfaceInfo interfaceInfo = interfaceInfoMapper.selectById(id);
        if (Objects.isNull(interfaceInfo)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        List<String> registered = dynamicApiConfig.getAlreadyRegistered(RequestMethod.POST);
        String apiPath = interfaceInfo.getApiPath();
        if (registered.contains(apiPath)) {
            return;
        }
        List<String> paths = new ArrayList<>();
        paths.add(apiPath);
        dynamicApiConfig.unregisterDynamicApiRoutesPOST(paths);
    }

    public void execute(String action, Long id, Integer reqType) {
        if (id <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (ApiOperationEnum.ONLINE_INTERFACE.getAction().equals(action)) {
            if (Objects.equals(InterfaceInfoEnum.REQ_TYPE_GET.getCode(), reqType)) {
                onlineGet(id);
            } else {
                onlinePost(id);
            }
        }
        if (ApiOperationEnum.ONLINE_INTERFACE.getAction().equals(action)) {
            if (Objects.equals(InterfaceInfoEnum.REQ_TYPE_POST.getCode(), reqType)) {
                offlinePost(id);
            } else {
                offlineGet(id);
            }
        }
    }

}
